package org.small.shop.phone.api.utils.datetime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static int getYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}

	public static Long getNewYearTimeMM() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		long dayMm = (24 * 60 * 60 * 1000);
		return System.currentTimeMillis()
				- (dayMm * month * 30 + dayMm * day + hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000);
	}

	public static Long getNewDayTimeMM() {
		Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		return System.currentTimeMillis() - (hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000);
	}

	// string类型转换为long类型
	// strTime要转换的String类型的时间
	// formatType时间格式
	// strTime的时间格式和formatType的时间格式必须相同
	public static long stringToLong(String strTime, String formatType) {
		Date date = stringToDate(strTime, formatType); // String类型转成date类型
		if (date == null) {
			return 0;
		} else {
			long currentTime = dateToLong(date); // date类型转成long类型
			return currentTime;
		}
	}

	// string类型转换为date类型
	// strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
	// HH时mm分ss秒，
	// strTime的时间格式必须要与formatType的时间格式相同
	public static Date stringToDate(String strTime, String formatType) {
		SimpleDateFormat formatter = new SimpleDateFormat(formatType);
		Date date = null;
		try {
			date = formatter.parse(strTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	// date类型转换为long类型
	// date要转换的date类型的时间
	public static long dateToLong(Date date) {
		return date.getTime();
	}

	// 获取当天0点时的毫秒值
	@SuppressWarnings("deprecation")
	public static Long getNewDateMM(Long time) {

		Date date = new Date(time);
		int hours = date.getHours();
		int minutes = date.getMinutes();
		int seconds = date.getSeconds();
		return (time - hours * 60 * 60 * 1000 - minutes * 60 * 1000 - seconds * 1000);
	}

	public static boolean isThanToDay(String strTime) {
		return stringToLong(strTime, "yyyy-MM-dd") > getNewDateMM(System.currentTimeMillis()) ? true : false;
	}

	public static boolean endTimeMoreStartTime(String startTime, String endTime) {
		return stringToLong(endTime, "yyyy-MM-dd") >= stringToLong(startTime, "yyyy-MM-dd") ? true : false;
	}
}
